<template>
    <div class="login-container">
      <div class="login-form-wrapper">
        <div class="login-header">
          <img src="../assets/logo.png" alt="StoryPlush Logo" class="logo" />
          <h2>StoryPlush管理后台</h2>
        </div>
        
        <el-form
          ref="formRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          status-icon
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              prefix-icon="User"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="Lock"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              class="login-button"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import { useUserStore } from '../stores/user'
  import { login } from '../api/user'
  
  // 状态管理
  const userStore = useUserStore()
  const router = useRouter()
  const formRef = ref(null)
  const loading = ref(false)
  
  // 表单数据
  const loginForm = reactive({
    username: '',
    password: ''
  })
  
  // 表单验证规则
  const loginRules = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
    ]
  }
  
  // 登录处理
  const handleLogin = () => {
    formRef.value.validate(async (valid) => {
      if (valid) {
        loading.value = true
        try {
          // 调用真实登录API
          const res = await login(loginForm)
          userStore.setToken(res.token)
          userStore.setUserInfo(res.user)
          ElMessage.success('登录成功')
          router.push('/')
        } catch (error) {
          ElMessage.error(error.message || '登录失败')
        } finally {
          loading.value = false
        }
      }
    })
  }
  </script>
  
  <style scoped>
  .login-container {
    height: 100vh;
    width: 100vw;
    background: linear-gradient(rgba(0,0,0,0.25), rgba(0,0,0,0.25)), url('/src/assets/admin-bg.jpg');
    background-size: cover;
    background-position: center 38%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  
  .login-form-wrapper {
    width: 400px;
    padding: 35px;
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(8px);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.3);
    transition: all 0.3s ease;
  }
  
  .login-form-wrapper:hover {
    box-shadow: 0 12px 36px rgba(0, 0, 0, 0.2);
    background: rgba(255, 255, 255, 0.75);
  }
  
  .login-header {
    text-align: center;
    margin-bottom: 30px;
  }
  
  .login-header h2 {
    color: #333;
    font-weight: 600;
    margin-top: 15px;
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.5);
  }
  
  .logo {
    width: 90px;
    margin-bottom: 15px;
    filter: drop-shadow(0 2px 5px rgba(0, 0, 0, 0.2));
    transition: transform 0.3s ease;
  }
  
  .logo:hover {
    transform: scale(1.05);
  }
  
  .login-form {
    margin-top: 30px;
  }
  
  .login-button {
    width: 100%;
    background: linear-gradient(135deg, #4e6fff 0%, #3e5cd8 100%);
    border: none;
    border-radius: 8px;
    padding: 12px 0;
    font-weight: 500;
    letter-spacing: 1px;
    box-shadow: 0 4px 12px rgba(78, 111, 255, 0.3);
    transition: all 0.3s ease;
  }
  
  .login-button:hover {
    background: linear-gradient(135deg, #5a7aff 0%, #4968e6 100%);
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(78, 111, 255, 0.4);
  }
  
  :deep(.el-input__wrapper) {
    background-color: rgba(255, 255, 255, 0.8);
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
  }
  
  :deep(.el-input__wrapper:hover) {
    border-color: rgba(78, 111, 255, 0.3);
  }
  
  :deep(.el-input__wrapper.is-focus) {
    box-shadow: 0 0 0 1px rgba(78, 111, 255, 0.3);
    border-color: rgba(78, 111, 255, 0.5);
  }
  
  :deep(.el-form-item__error) {
    color: rgba(245, 108, 108, 0.9);
    font-weight: 500;
    text-shadow: 0 0 1px rgba(255, 255, 255, 0.5);
  }
  </style>